<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="课堂标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入课堂标题" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['kenuo:course:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['kenuo:course:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['kenuo:course:remove']">删除</el-button>
      </el-col>
      <!--  <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['kenuo:course:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="courseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="课堂标题" align="center" prop="title" />
      <el-table-column label="概括内容" align="center" prop="myContent" />
      <el-table-column label="课堂封面图" align="center" prop="url">
        <template slot-scope="scope">
          <el-image style="width: 50px; height: 50px" v-if="scope.row.url" :src="scope.row.url" fit=""></el-image>
          <div v-else>暂无</div>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="ktType">
        <template slot-scope="scope">
          {{scope.row.ktType == "1" ? "图文" : "视频"}}
        </template>
      </el-table-column>
      <!-- <el-table-column label="来源" align="center" prop="source" /> -->
      <el-table-column label="创建人" align="center" prop="creator" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="修改时间" align="center" prop="updateTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['kenuo:course:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['kenuo:course:remove']">删除</el-button>
          <!-- 1:上架  2：下架 -->
          <el-button size="mini" type="text" :icon="scope.row.isOn == 2 ? 'el-icon-top' : 'el-icon-bottom'"
            @click="setFunc(scope.row)">{{scope.row.isOn == 2 ? '上架' : '下架'}}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" v-if="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="课堂标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入课堂标题" />
        </el-form-item>
        <el-form-item label="概况" prop="myContent">
          <el-input type="textarea" :rows="2" placeholder="请输入内容" clearable="true" v-model="form.myContent">
          </el-input>
        </el-form-item>
        <el-form-item label="上传课堂封面图" prop="url">
          <!-- <el-input v-model="form.url" placeholder="请上传封面图" /> -->
          <UploadImg v-model="form.url" @operateImg="operateImg" uploadType="png,jpg,jpeg" :imgUrl="imgUrl"></UploadImg>
          <el-link type="danger" disabled>注意：封面图尺寸只能是512 * 300,大小不超过2M</el-link>
        </el-form-item>
        <el-form-item label="类型" prop="ktType">
          <el-radio-group v-model="form.ktType" size="small" @change="getuploadType">
            <el-radio label="1">图文</el-radio>
            <el-radio label="2">视频</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="内容" prop="des" v-if="showType">

          <!-- <editor v-model="form.des" :min-height="192" placeholder="请输入内容说明" /> -->
          <Editor v-model="form.des" :value="form.des" :fileSize="fileSize"></Editor>
          <el-link type="danger" disabled>注意：图片大小不超过2M</el-link>
        </el-form-item>
        <el-form-item label="上传视频" prop="des" v-else>
          <UploadFiles v-model="form.des" :fileList="fileList" @operateFiles="operateFiles" :uploadType="uploadType">
          </UploadFiles>
          <el-link type="danger" disabled>{{des}}</el-link>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listCourse,
    getCourse,
    delCourse,
    addCourse,
    updateCourse
  } from "@/api/kenuo/course";
  import UploadFiles from '@/views/components/uploadFiles.vue';
  import Editor from '@/components/Editor/index';
  import UploadImg from './uploadImg'
  export default {
    name: "Course",
    components: {
      UploadFiles,
      UploadImg
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 【请填写功能名称】表格数据
        courseList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          title: null,
          url: null,
          ktType: null,
          source: null,
          creator: null,
          isOn: null,
          des: null,
          myContent: null,
          remarkOne: null,
          remarkTwo: null,
          remarkThree: null
        },
        showType: true,
        fileList: [],
        uploadType: '.mp4,.flv',
        des: '视频不大于200M且类型为.mp4或.flv，(注意：最多上传一个文件)',
        imgUrl: '',
        fileSize: 2,
        // 表单参数
        form: {
          ktType: "1",
        },
        // 表单校验
        rules: {
          title: [{
            required: true,
            message: "课堂标题不能为空",
            trigger: "blur"
          }],
          url: [{
            required: true,
            message: "请上传课堂封面图,封面图尺寸只能是512 * 300,大小不超过2M",
            trigger: "blur"
          }],
          myContent: [{
            required: true,
            message: "概况不能为空",
            trigger: "blur"
          }],
          ktType: [{
            required: true,
            message: "类型不能为空",
            trigger: "blur"
          }],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      operateImg(val) {
        this.imgUrl = val
        this.form.url = val
        console.log(this.form)
        console.log(this.imgUrl)
      },
      operateFiles(val) {

        this.fileList = val
        if (val.length > 0) {
          this.form.des = val[0].url
        } else {
          this.form.des = ''
        }

      },
      //切换图文类型
      getuploadType(e) {
        console.log(this.fileList)
        if (e == "1") {
          if (this.fileList.length > 0) {
            this.form.des = ''
          }
          this.showType = true
        } else {
          if (this.fileList.length > 0) {
            this.form.des = this.fileList[0].url
          } else {
            this.form.des = ""
          }

          this.showType = false
        }
      },
      /** 查询【请填写功能名称】列表 */
      getList() {
        this.loading = true;
        listCourse(this.queryParams).then(response => {
          this.courseList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          title: null,
          url: null,
          ktType: null,
          creator: null,
          des: null,
          myContent: null,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.imgUrl = ""
        this.showType = true
        this.fileList = []
        this.open = true;
        this.title = "添加";
      },
      //
      setFunc(row) {

        let obj = {
          id: row.id,
          isOn: row.isOn == "2" ? "1" : "2"
        }
        console.log(obj)

        updateCourse(obj).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.open = false;
          this.getList();
        });
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getCourse(id).then(response => {
          this.form = response.data;
          this.form.ktType = String(this.form.ktType)
          this.imgUrl = this.form.url
          if (this.form.ktType == "1") {
            this.showType = true
          } else {
            this.showType = false
            if (this.form.des != "") {
              this.fileList = [{
                name: '课堂视频文件',
                url: response.data.des
              }]
            }
          }
          this.open = true;
          this.title = "修改";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            console.log(this.form)
            // return
            if (this.form.id != null) {
              updateCourse(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addCourse(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除该数据项？').then(function() {
          return delCourse(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('kenuo/course/export', {
          ...this.queryParams
        }, `course_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
